/*!
 * Main v1.0.0. 
 * 公用js
 */
//定于全局域名空间
var TuHaoValid = window.TuHaoValid || {};
TuHaoValid.LoginUrl = "/th/login";
TuHaoValid.required = "不能为空";

TuHaoValid.Validator = new function () {
    var self = this;
    self.input = function (validObject, e) {
        var $formId = $(this);
        //requestObject.url|| TuHao.LoginUrl
        validObject.formId ||
            //必填项
        $formId.find("input").each(function (i, o) {
            var $o = $(o);//当前元素
            var className = $o.css("tuHaoValid");
        });
    };
};

/**
 * onValidationComplete验证通过后操作
 * @param validObject
 * @constructor
 */
$.fn.TuHaoValid = function (validObject) {

    var $formId = $(this);
    if (TuHao.Tool.isBlank($formId)) {
        (validObject.onValidationComplete && typeof(validObject.onValidationComplete) === "function") && validObject.onValidationComplete();
        return;
    }
    var $inputs = $formId.find("input[data-validator],textarea[data-validator]");
    if (TuHao.Tool.isBlank($inputs)) {
        (validObject.onValidationComplete && typeof(validObject.onValidationComplete) === "function") && validObject.onValidationComplete();
        return;
    }

    for(var i = 0; i < $inputs.length; i++){
        var $input = $inputs.eq(i);
        validObject.push("input",$input);
        var result = TuHaoValid.Common.alertInput(validObject);
        if (result){
            continue;
        }else{
            return;
        }
    }

    /*for (var j = 0; j < $inputs.length; j++) {
        var $input = $inputs.eq(j);//当前元素
        var classNameStr = $input.attr("class");
        if (TuHao.Tool.isBlank(classNameStr)) {
            continue;
        }
        var classNames = classNameStr.split(" ");
        var validClassNames;
        if (TuHao.Tool.isBlank(classNames)) {
            continue;
        }
        for (var i = 0; i < classNames.length; i++) {
            var classNameOne = classNames[i];
            if (classNameOne.indexOf("tuHaoValid") > -1) {
                validClassNames = classNameOne.substring(classNameOne.indexOf("[") + 1, classNameOne.length - 1).split(",");
            }
        }
        if (TuHao.Tool.isBlank(validClassNames) || validClassNames.length <= 0) {
            continue;
        }
        for (var t = 0; t < validClassNames.length; t++) {
            var validClassName = validClassNames[t];
            if (validClassName.trim() == "required" && $input.val().isBlank()) {//校验不可为空//必填项
                var valid = $input.data("valid");
                if (!TuHao.Tool.isBlank(valid)) {
                    TuHao.Layer.validAlert(valid + TuHaoValid.required);
                    return;
                } else {
                    TuHao.Layer.validAlert(TuHaoValid.required);
                    return;
                }
            }
            //添加校验在这判断
        }
        validClassNames = [];
    }*/
    (validObject.onValidationComplete && typeof(validObject.onValidationComplete) === "function") && validObject.onValidationComplete();
};

TuHaoValid.Common = new function () {
    var self = this;

    self.alertInput = function (validObject) {
        var $input = validObject.input;
        var validatorStr = $input.data("validator");
        if(TuHao.Tool.isBlank(validatorStr)){
            return true;
        }
        var validators = validatorStr.split(",");
        for(var j = 0;j<validators.length;j++){
            var validator = validators[j].trim();
            var myAlertTest = $input.data("valid");
            var result = TuHaoValid.Common.alertText($.extend({
                "validator":validator,
                "myAlertTest":myAlertTest,
                "value":$input.val()
            },validObject));
            if (result){
                continue;
            }else{
                return false;
            }
        }
        return true;
    };

    self.alertText = function (validObject) {

        if(TuHao.Tool.isBlank(TuHao.validationEngine_zh_CN[validObject.validator])){
            alert("前端配置异常");
            return false;
        }
        validObject = $.extend(validObject,TuHaoValid.Common.common(validObject));
        var result;
        try{
            result = window.TuHaoValid.Common[validObject.validator](validObject);
        }catch(ex){
            console.log(ex.message);//t is not defined
            console.log(ex.name);//ReferenceError
            if(ex.name === "TypeError"){
                result = TuHaoValid.Common.regex(validObject);
            }else{
                throw ex;
            }
        }

        if(TuHao.Tool.isBlank(result)){
            return true;
        }
        if(validObject.alertType === 2){//弹框
            alert(result);
        }else{
            //样式自定义

        }
        return false;
    };

    self.common = function (validObject) {
        var validator = validObject.validator;
        var validatorJson = TuHao.validationEngine_zh_CN[validator];
        var regex = validatorJson["regex"];
        var alertText = validatorJson["alertText"].trim();
        var alertText2 = TuHao.Tool.trim(validatorJson["alertText2"]);
        var myAlertTest = validObject.myAlertTest;
        var alertType = validObject.alertType;
        if(TuHao.Tool.isBlank(myAlertTest)){
            myAlertTest = "";
        }

        if(TuHao.Tool.isBlank(alertType)){
            alertType = validObject.input.data("alertType");
        }

        if(TuHao.Tool.isBlank(alertType)){
            alertType = 2;//默认
        }
        return {
            "regex": regex,
            "myAlertTest": myAlertTest,
            "alertText": alertText,
            "alertText2": alertText2,
            "alertType":alertType,
            "input":validObject.input
        };
    };

    self.required = function (validObject) {
        if(TuHao.Tool.isBlank(validObject.value)){
            return validObject.myAlertTest+validObject.alertText;
        }else{
            return "";
        }
    };

    self.phone = function (validObject) {
        var regex = validObject.regex;
        var reg = new RegExp(regex);
        var result = TuHaoValid.Common.required(validObject);
        if(!TuHao.Tool.isBlank(result)){
            return result;
        }
        var flag = validObject.value.match(reg);
        if(flag){
            return "";
        }
        return validObject.myAlertTest+validObject.alertText;
    }

    self.regex = function (validObject) {
        var regex = validObject.regex;
        var reg = new RegExp(regex);
        var result = TuHaoValid.Common.required(validObject);
        if(!TuHao.Tool.isBlank(result)){
            return result;
        }
        var flag = validObject.value.match(reg);
        if(flag){
            return "";
        }
        return validObject.myAlertTest+validObject.alertText;
    }
};

$(function () {
    var $inputs = $("input[data-validator],textarea[data-validator]");
    $inputs.blur(function () {
        var $this = $(this);
        TuHaoValid.Common.alertInput({
            "input":$this
        });
    });
});

function a(t) {
    alert(t);
}

